local pointsMarkers={
	{1,-2520.81,2354.00,4.98, -2526.87,2355.88,4.98},
	--{2,1058.57,2368.92,10.82},
	--{3,525.44,-1299.12,17.20},
}

local spawnMarkers={
	[1] = {-2515.31,2355.81,5.08,-0.65,0.04,0.37},
	--{2,1058.57,2368.92,10.82},
	--{3,525.44,-1299.12,17.20},
}

for i,v in ipairs(pointsMarkers) do
	local marker=createMarker(v[2], v[3], v[4], "cylinder", 2, 255, 0, 255, 0)
    setElementData(marker,"przechowalnia_marker:id",tonumber(v[1]))
    setElementData(marker, 'marker:data', {name='Przechowalnia', icon='garage',size=1})
    setElementData(marker,"przechowalnia_resp",{spawnMarkers[v[1]][1],spawnMarkers[v[1]][2],spawnMarkers[v[1]][3],spawnMarkers[v[1]][4],spawnMarkers[v[1]][5],spawnMarkers[v[1]][6]})
    createBlip(v[2], v[3], v[4],35)
	local marker_oddaj=createMarker(v[5], v[6], v[7], "cylinder", 5, 255,255,255,0)
    setElementData(marker_oddaj,"przechowalnia_oddaj_marker:id",tonumber(v[1]))
    setElementData(marker_oddaj, 'marker:data', {name='Oddawanie pojazdów', icon='cars', size=3})
end

function open(el,md)
if not md or getElementType(el) ~= "player" then return end
        if isPedInVehicle(el) then return end
        local code=getElementData(source,"przechowalnia_marker:id")
        if not code then return end
       	triggerClientEvent(el,'przechowalnia:showGUI',resourceRoot, getElementData(source,"przechowalnia_resp"))
		setPlayerHudComponentVisible(el,"radar",false)
	   -- exports["PSSM-interiory"]:addloading(el,"1")
end
addEventHandler( "onMarkerHit", resourceRoot, open)
 
addEvent("onParkingGetRekords", true)
addEventHandler("onParkingGetRekords", root, function()
    local uid=getElementData(source,"player:data").uid
    if not uid then return end
	result=exports["cmta-db"]:dbGet("select * from cmta_pojazdy WHERE ownedPlayer=? AND parking=1",getElementData(source,"player:data").uid)
    if result and #result > 0 then
        triggerClientEvent(source, "onParkingGetVehicles", source, result)
    end
end)

addEventHandler("onMarkerHit", resourceRoot, function(el,md)
    if getElementType(el) == "player" then
        if isPedInVehicle(el) then return end
        local code=getElementData(source,"przechowalnia_oddaj_marker:id")
        if not code then return end
        local veh=getPedOccupiedVehicle(el)
        if not veh then return end
        if getVehicleController(veh) ~= el then return end
        local id=getElementData(veh,"vehicle:id")
        if not id then return end
        if isTimer(timeroddania) then return end
        setElementFrozen(veh,true)
        outputChatBox("* Trwa oddawanie  pojazdu do przechowalni...", el,255,255,255,true)
        toggleControl(el,"enter_exit",false)
        timeroddania=setTimer(function()
			setElementFrozen(veh,false)
			local id=getElementData(veh,"vehicle:id")
			if not id then return end
            if getElementData(veh,"vehicle:spoiler") then
                destroyElement(getElementData(veh,"vehicle:spoiler"))
            end
            exports["cmta-systempojazdow"]:onSaveVehicle(veh)
            destroyElement(veh)
            exports["cmta-db"]:dbSet("UPDATE cmta_pojazdy SET parking=1 WHERE id=?", id)
            toggleControl(el,"enter_exit",true)
            outputChatBox("* Pomyślnie zapisano pojazd.", el, 255, 255 ,255 ,true)
        end, 3000, 1)
	elseif getElementType(el) == "vehicle" then
		local peds= getVehicleOccupants(el)
		if #peds > 0 then return end
		local id=getElementData(el,"vehicle:data").id
        if not id then return end
        if isTimer(timeroddania) then return end
        
        timeroddania=setTimer(function()
			setElementFrozen(el,false)
			local id=getElementData(el,"vehicle:data").id
			if not id then return end
            if getElementData(el,"vehicle:spoiler") then
                destroyElement(getElementData(el,"vehicle:spoiler"))
            end
            exports["cmta-systempojazdow"]:onSaveVehicle(id)
            destroyElement(el)
            exports["cmta-db"]:dbSet("UPDATE cmta_pojazdy SET parking=1 WHERE id=?", id)
        end, 3000, 1)
		setElementFrozen(el,true)
		setVehicleLocked(el,true)
    end
end)

addEvent('przechowalnia:hideGUI',true)
addEventHandler('przechowalnia:hideGUI',resourceRoot,function()
	if isElement(getElementData(client,"przechoveh:vehp")) then
    destroyElement(getElementData(client,"przechoveh:vehp"))
	end
    if isElement(getElementData(client,"vehicle_przecho:spoiler")) then
        destroyElement(getElementData(client,"vehicle_przecho:spoiler"))
    end
end)


addEvent("createvehicles:parking", true)
addEventHandler("createvehicles:parking", resourceRoot, function(id)
	local resulted=exports["cmta-db"]:dbGet("SELECT * FROM cmta_pojazdy WHERE id=? LIMIT 1;",id)
    if isElement(getElementData(client,"vehicle_przecho:spoiler")) then
        destroyElement(getElementData(client,"vehicle_przecho:spoiler"))
    end
    if isElement(getElementData(client,"przechoveh:vehp")) then    
    destroyElement(getElementData(client,"przechoveh:vehp"))
	end
	for i,v in pairs(resulted) do
	local color=split(v["color"], ",")
	local show_vehiclep = createVehicle(tonumber(v["model"]),-2049.18, 466.93, 301.17,0,0,0)
	setElementData(client,"przechoveh:vehp",show_vehiclep)
	setElementDimension(show_vehiclep,getElementData(client,"player:data").uid)
	setVehicleColor(show_vehiclep, color[1], color[2], color[3], color[4],color[5], color[6], color[7], color[8],color[9], color[10], color[11], color[12])
	setVehiclePlateText(show_vehiclep, tostring(""..v["id"]))
	for i2,v2 in ipairs(split(v["tuning"], ",")) do addVehicleUpgrade(show_vehiclep, v2) end
	if v["shader"] ~= 0 then
        triggerClientEvent(client,'przechowalnia:paintjob',resourceRoot,v["shader"],show_vehiclep)
    end
    if v["spoiler"] ~= 0 then
        local spoiler=split(v["spoilerOffsets"], ",")
        local spojler = createObject( v["spoiler"], -1745.15491, -137.97495, 3.55469+0.60)
        setElementDimension(spojler,1)
        attachElements ( spojler, show_vehiclep, spoiler[1],spoiler[2],spoiler[3])
        setObjectScale(spojler,1)
        setElementData(client, "vehicle_przecho:spoiler", spojler)
        end
    end
end)

function quitPlayer ( )
    if isElement(getElementData(source,"przechoveh:vehp")) then
        destroyElement(getElementData(source,"przechoveh:vehp"))
    end
    if isElement(getElementData(source,"vehicle_przecho:spoiler")) then
        destroyElement(getElementData(source,"vehicle_przecho:spoiler"))
    end
end
addEventHandler ( "onPlayerQuit", root, quitPlayer )

addEvent("onParkingVehicleSpawn", true)
addEventHandler("onParkingVehicleSpawn", root, function(id,s)
    if not id then return end
    if isElement(getElementData(source,"vehicle_przecho:spoiler")) then
        destroyElement(getElementData(source,"vehicle_przecho:spoiler"))
    end
	if s == "1" then
    local spawn=exports["cmta-systempojazdow"]:onRespawnVehicles(id,spawnMarkers[1],source)
	elseif s == "2" then
    local spawn=exports["cmta-systempojazdow"]:onRespawnVehicles(id,{-2047.63, 464.46, 34.72, -0.00, 0.00, 270.23},source)
    end
       outputChatBox("* Pomyślnie przywrócono pojazd.", source, 255, 255 ,255 ,true)
    if spawn then
	end
end)